/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.retail;

import com.xtd.facede.retail.AccountFacade;
import com.xtd.facede.retail.LeadFacade;
import com.xtd.model.retail.Lead;
import com.xtd.util.DateTimeUtil;
import com.xtd.view.AbstractManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@SessionScoped
public class LeadManager extends AbstractManager {

    @EJB
    LeadFacade leadFacade;
    @EJB
    AccountFacade accountFacade;
    private List<Lead> leads;
    private Lead selectedLead;
    private int latestDateNumber;
    private String searchContent;
    @ManagedProperty(value = "#{accountManager}")
    private AccountManager accountManager;

    /** Creates a new instance of LeadManager */
    public LeadManager() {
    }

    /**
     * @return the leads
     */
    public List<Lead> getLeads() {
        if (leads == null) {
            leads = new ArrayList<Lead>();
        }
        return leads;
    }

    /**
     * @param leads the leads to set
     */
    public void setLeads(List<Lead> leads) {
        this.leads = leads;
    }

    /**
     * @return the selectedLead
     */
    public Lead getSelectedLead() {
        return selectedLead;
    }

    /**
     * @param selectedLead the selectedLead to set
     */
    public void setSelectedLead(Lead selectedLead) {
        this.selectedLead = selectedLead;
    }

    /**
     * @return the latestDateNumber
     */
    public int getLatestDateNumber() {
        return latestDateNumber;
    }

    /**
     * @param latestDateNumber the latestDateNumber to set
     */
    public void setLatestDateNumber(int latestDateNumber) {
        this.latestDateNumber = latestDateNumber;
    }

    /**
     * @return the searchContent
     */
    public String getSearchContent() {
        return searchContent;
    }

    /**
     * @param searchContent the searchContent to set
     */
    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
    }

    public void search(ActionEvent actionEvent) {
        this.getLeads().clear();
        this.getLeads().addAll(leadFacade.find(searchContent));

    }

    public void createLead(ActionEvent actionEvent) {
        this.selectedLead = new Lead();


    }

    public void save(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        if (selectedLead.getAccount().getId() == null) {
            List existingAccounts = accountFacade.findDuplicatePhone(selectedLead.getAccount());
            if (existingAccounts != null && existingAccounts.size() > 0) {
                getAccountManager().getAccounts().clear();
                getAccountManager().getAccounts().addAll(existingAccounts);
                context.addCallbackParam("duplicate", true);

                addMessage("保存失败", "请从弹出窗口处，选定已有客户。", FacesMessage.SEVERITY_WARN);
                return;
            }
        }
        boolean isNew = selectedLead.isNew();


        try {
            leadFacade.update(selectedLead);
            addMessage("保存成功", null);
            context.addCallbackParam("success", true);
            if (isNew) {
                this.leads.add(selectedLead);
            }
        } catch (Exception e) {
            e.printStackTrace();
            getLogger(this.getClass()).log(Level.FINEST, "客户档案更新失败", e);
            addMessage("保存失败", "请稍候再试", FacesMessage.SEVERITY_ERROR);
            context.addCallbackParam("success", false);
        }
    }

    public void cancellCreate(CloseEvent actionEvent) {
        addMessage("取消保存", null);
    }

    public void findToday(ActionEvent actionEvent) {
        System.out.println(DateTimeUtil.getDateBefore(new Date(), 0));
        this.leads.clear();
        this.leads.addAll(this.leadFacade.find(DateTimeUtil.getDateBefore(new Date(), 0), DateTimeUtil.getDateEnd(new Date())));
    }

    public void findWeek(ActionEvent actionEvent) {
        System.out.println(DateTimeUtil.getWeekStart());
        this.leads.clear();
        this.leads.addAll(this.leadFacade.find(DateTimeUtil.getWeekStart(), DateTimeUtil.getDateEnd(new Date())));
    }

    public void findMonth(ActionEvent actionEvent) {
        System.out.println(DateTimeUtil.getMonthStart());
        this.leads.clear();
        this.leads.addAll(this.leadFacade.find(DateTimeUtil.getMonthStart(), DateTimeUtil.getDateEnd(new Date())));
    }

    /**
     * @return the accountManager
     */
    public AccountManager getAccountManager() {
        return accountManager;
    }

    /**
     * @param accountManager the accountManager to set
     */
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }
}
